import xlrd

def read_excel():
    filename = '.\citycode.xls'
    data = xlrd.open_workbook(filename)
    table = data.sheet_by_name('Sheet1')
    row_num = table.nrows  # 行数
    col_num = table.ncols  # 列数
    datas = dict([]) # 这步也要转字典类型
    for i in range(row_num):
        ee = (table.row_values(i))

        xx = {ee[0]: [ee[1], ee[2]]}  # dict([table.row_values(i)]) # 这一步就要给它转字典类型，不然update没法使用
        datas.update(xx)
    return datas
    # print("字典中保存的学历：", datas[key])

# def read_excel():
#     """ 转换excel表为字典 """
#     # 打开excel表，填写路径
#     file = '.\citycode.xlsx'
#     book = xlrd.open_workbook(file)
#     # 找到sheet页
#     table = book.sheet_by_name("Sheet1")
#     # 获取总行数、总列数
#     row_Num = table.nrows
#     col_Num = table.ncols
#
#     s =[]
#     key =table.row_values(0)# 这是第一行数据，作为字典的key值
#
#     if row_Num <= 1:
#         print("没数据")
#     else:
#         j = 1
#         for i in range(row_Num-1):
#             d ={}
#             values = table.row_values(j)
#             for x in range(col_Num):
#                 # 把key值对应的value赋值给key，每行循环
#                 d[key[x]]=values[x]
#             j+=1
#             # 把字典加到列表中
#             s.append(d)
#         return s
#
# if __name__ == '__main__':
#     s=read_excel()
#     for i in s:
#         print(i)